package triangleWorm;

import java.util.Random;

import processing.core.*;

public class WormElement {
	
	private PApplet p;
	
	private PVector[] points = new PVector[3];
	
	private float red;
	private float green;
	private float blue;
	
	private float alpha = 0;
	private float alphaFadeUp = 255f / 8f;
	private boolean isFadingOut = false;
	private float fadeDownSpeed = alphaFadeUp;
	private boolean isDead = false;
	
	public WormElement() {
		
	}
	
	public void init(PApplet processing) {
		p = processing;
	}
	
	public void set(PVector point1, PVector point2, PVector point3, int color, float alpha) {
		points[0] = point1;
		points[1] = point2;
		points[2] = point3;
		
		red = p.red(color);
		green = p.green(color);
		blue = p.blue(color);
		
		this.alpha = alpha;
	}
	
	public void set(PVector[] points, int color, float alpha) {
		set(points[0], points[1], points[1], color, alpha);
	}
	
	public void set(WormElement triangle) {
		set(triangle.getPoints()[0], triangle.getPoints()[1], triangle.getPoints()[2], triangle.getColor(), triangle.getAlpha());
	}
	
	public void draw() {
		p.stroke(red, green, blue, alpha * 2);
		p.beginShape(PApplet.TRIANGLES);
			p.fill(red + 20, green + 20, blue + 20, alpha);
			p.vertex(points[0].x, points[0].y, points[0].z);
			p.fill(red, green, blue, alpha);
			p.vertex(points[1].x, points[1].y, points[1].z);
			p.fill(red - 10, green - 10, blue - 10, alpha);
			p.vertex(points[2].x, points[2].y, points[2].z);
		p.endShape();
		
		if(alpha < 255 && !isFadingOut) {
			alpha += alphaFadeUp;			
		} else if (isFadingOut) {
			alpha -= fadeDownSpeed;
			if(alpha <= 0) {
				isDead = true;
			}
		}
	}
	
	public PVector[] getTwoClosesPoints(PVector point) {
		PVector[] closesPoints = new PVector[2];
		float closestDist = Float.MAX_VALUE;
		float nextClosestDist = Float.MAX_VALUE;
		
		float dist;
		for(int i = 0; i < points.length; i ++) {
			dist = point.dist(points[i]);
			
			if(dist < closestDist) {
				closesPoints[1] = closesPoints[0];
				closesPoints[0] = points[i];
				
				nextClosestDist = closestDist;
				closestDist = dist;
			} else if(dist < nextClosestDist) {
				closesPoints[1] = points[i];
				
				nextClosestDist = dist;
			}
		}
		
		return closesPoints;
	}
	
	public PVector[] getPoints() {
		return points;
	}
	
	public int getColor() {		
		return p.color(red, green, blue);
	}
	
	public float getAlpha() {
		return alpha;
	}
	
	public void setAlpha(float value) {
		alpha = value;
	}
	
	public void kill() {
		isFadingOut = true;
		fadeDownSpeed = 3f + (float)Math.random() * 20f;
	}
	
	public boolean isKilledToDeath() {
		return isDead;
	}
	
	public String toString() {
		String value = "";
		value += "Triangle number" + this.hashCode() + "\n";
		value += "Points: \n";
		value += "   0: " + points[0] + "\n";
		value += "   1: " + points[1] + "\n";
		value += "   2: " + points[2] + "\n";
		value += "Color: r:" + red + " g:" + green + " b:" + blue + "\n";
		value += "Alpha: " + alpha;
		
		return value;
	}
}